import Axios from 'axios';
import Vue from 'vue';

const axiosInstance = Axios.create();

axiosInstance.defaults.timeout = 10000;
axiosInstance.defaults.headers.post['Content-Type'] = 'application/json';

const http = {
    get(url, params){
        return new Promise((resolve, reject) =>{
            axiosInstance.get(url,{params: params}).then(res => {
                resolve(res.data);
            }).catch(err =>{
                reject(err.data);
            })
        });
    },
    post(url, params) {
        return new Promise((resolve, reject) => {
            axiosInstance.post(url, params).then(res => {
                resolve(res.data);
            }).catch(err =>{
                reject(err);
            })
        });
    },
    put(url, params) {
        return new Promise((resolve, reject) => {
            axiosInstance.put(url, params).then(res => {
                resolve(res.data);
            }).catch(err =>{
                reject(err.data);
            })
        });
    },
    delete(url, params) {
        return new Promise((resolve, reject) => {
            axiosInstance.delete(url, params).then(res => {
                resolve(res.data);
            }).catch(err =>{
                reject(err.data);
            })
        });
    }
}

Vue.prototype.$http = http;